package com.business.action.admin.system;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.system.UserBean;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.boot.BootStart;
import net.sf.rose.boot.BootStrap;
import net.sf.rose.boot.bean.BootStrapBean;
import net.sf.rose.boot.bean.StaticLocationBean;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.util.BeanWrapper;
import net.sf.rose.util.RoseRuntimeException;
import net.sf.rose.util.StringUtil;
import net.sf.rose.web.util.WebUtils;

@Controller
@RequestMapping("/admin/boot")
public class BootStrapAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/any/login.do")
	public PageBean login(HttpServletRequest request, UserBean bean) {
		PageBean res = new PageBean();
		if (bean.getLoginName() == null) {
			res.setSuccess(false);
			res.setMessage("用户登录名不能为空");
			return res;
		} else if (bean.getLoginPassword() == null) {
			res.setSuccess(false);
			res.setMessage("用户密码不能为空");
			return res;
		}
		if (!bean.getLoginName().equals("admin") || !bean.getLoginPassword().equals("784533")) {
			res.setSuccess(false);
			res.setMessage("用户或密码不正确");
			return res;
		}
		bean.setUserName("超级管理员");
		String token = WebUtils.setUserBeanCache(request, bean);
		res.getDataMap().put("_token", token);
		res.setData(bean);
		return res;
	}

	/**
	* 
	*/
	@ResponseBody
	@RequestMapping("/web/list.do")
	public PageBean listLocation(HttpServletRequest request) {
		PageBean page = new PageBean();
		page.setData(BootStrap.getBootBean().getLocationList());
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/web/save.do")
	public PageBean saveLocation(HttpServletRequest request, String json) {
		PageBean page = new PageBean();
		List<StaticLocationBean> list = StringUtil.parseJsonArray(StaticLocationBean.class, json);
		BootStrapBean bean = BootStrap.getBootBean();
		bean.setLocationList(list);
		bean.save();
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getStartList.do")
	public PageBean getStartList(HttpServletRequest request) {
		PageBean page = new PageBean();
		Map<String, BootStart> map = BootStrap.getBootBean().getBootStartMap();
		List<Map<String, Object>> list = new ArrayList<>();
		for (BootStart boot : map.values()) {
			Map<String, Object> m = new HashMap<>();
			m.put("name", boot.getClass().getName());
			m.put("start", boot.needStart());
			list.add(m);
		}
		page.setData(list);
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getStartDetail.do")
	public PageBean getStartDetail(HttpServletRequest request, String name) {
		PageBean page = new PageBean();
		Map<String, BootStart> map = BootStrap.getBootBean().getBootStartMap();
		String key = name.replace('.', '-');
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> nameMap = new HashMap<>();
		nameMap.put("key", "name");
		nameMap.put("val", name);
		list.add(nameMap);
		BootStart boot = map.get(key);
		Class<?> clazz = boot.getClass();
		try {
			for (Field f : clazz.getDeclaredFields()) {
				boolean flag = false;
				if (f.getAnnotation(Element.class) != null) {
					flag = true;
				} else if (f.getAnnotation(Attribute.class) != null) {
					flag = true;
				}
				if (flag) {
					key = f.getName();
					f.setAccessible(true);
					nameMap = new HashMap<>();
					nameMap.put("key", key);
					// System.out.println(f.get(boot));
					nameMap.put("val", f.get(boot));
					list.add(nameMap);
				}
			}
		} catch (Exception e) {
			throw new RoseRuntimeException(e);
		}
		page.setData(list);
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/saveStartDetail.do")
	public PageBean saveStartDetail(HttpServletRequest request) {
		PageBean page = new PageBean();
		String[] arr = null;
		Map<?, ?> m = request.getParameterMap();
		BootStrapBean bean = BootStrap.getBootBean();
		Map<String, BootStart> map = bean.getBootStartMap();
		arr = (String[]) m.get("name");
		String name = arr[0];
		name = name.replace('.', '-');
		BootStart boot = map.get(name);
		BeanWrapper bw = new BeanWrapper();
		bw.setWrappedInstance(boot);
		for (Object key : m.keySet()) {
			arr = (String[]) m.get(key);
			bw.set(key.toString(), arr[0]);
		}
		boot.start();
		bean.save();
		return page;
	}

}
